/*
 * Project: Hell Watch
 * Author: pajoke
 * Copyright: (C) 2014 Hell Prototypes
 * License: GNU GPL v3 (see License.txt)
 * Web: http://www.hellprototypes.com
 */

// SPI driver

#include <avr/io.h>
#include <avr/power.h>
#include "common.h"
#include "drivers/spi.h"

#define MISO	B4
#define MOSI	B3
#define SCK		B5
#define SS		B2


void spi_init()
{
	ROM_Active();
	spi_play_one_byte(0xB9);//Power-down
	ROM_Deactive();

	SET_SCK();
	SET_MOSI();

	PR.PRPC  = 0x3C;        // Stop: USART0, USART1, HIRES,SPI,TC1
}

byte spi_play_one_byte(byte data)
{
	byte ret_val = 0, mask = 0x80;

	do {
		if(mask & data) {
			SET_MOSI();
		} else {
			CLR_MOSI();
		}
		//delay();
		TOGGLE_SCK();
		//delay();
		if(GET_MISO()) {
			ret_val |= mask;
		}
		mask = mask >> 1;
		TOGGLE_SCK();
		//delay();
	} while(mask);

	return ret_val;
}

void spi_enable()
{

}

void spi_disable()
{

}